"""
Case Type   : datakit并行安装数据库
Case Name   : 创建并行安装任务，点击主机ip下拉框，显示已导入的服务器
Create At   : 2024/12/16
Owner       : @lonely-dance
Description :
    1.导入服务器
    2.进入并行安装任务界面
    3.查看可选择的服务器
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.datakit_api.BaseOpsApi import BaseOpsApi


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.base_ops_api = BaseOpsApi(self.token)
        self.pri_node = Node('PrimaryDbUser')
        self.pri_node_root = Node('PrimaryRoot')
        self.server_name = 'server_001'
        self.data1 = {"isRemember": True,
                      "password": f"{self.pri_node_root.ssh_password}",
                      "port": 22,
                      "privateIp": f"{self.pri_node_root.ssh_host}",
                      "publicIp": f"{self.pri_node_root.ssh_host}",
                      "remark": "",
                      "username": f"{self.pri_node_root.ssh_user}"}
        self.log.info(self.data1)
        self.data2 = {"hostId": "",
                      "isRemember": True,
                      "name": f"{self.server_name}",
                      "password": f"{self.pri_node_root.ssh_password}",
                      "port": 22,
                      "privateIp": f"{self.pri_node_root.ssh_host}",
                      "publicIp": f"{self.pri_node_root.ssh_host}",
                      "remark": "",
                      "tags": [],
                      "username": f"{self.pri_node_root.ssh_user}"}
        self.log.info(self.data2)

    def test_batch_installation(self):
        text = '----step1:导入服务器 expect:成功----'
        self.log.info(text)
        self.log.info('----联通性测试----')
        res1 = self.com_api.post_host_ping(self.data1)
        self.log.info(res1.json())
        self.assertEqual(200, res1.json()['code'], "连接信息不可用")
        self.log.info('----导入服务器----')
        res2 = self.com_api.post_host(self.data2)
        self.log.info(res2.json())
        self.assertEqual(200, res2.json()['code'], "导入服务器失败")
        self.log.info('----查看服务器导入是否成功----')
        res3 = self.com_api.get_host_page(name=f"{self.pri_node_root.ssh_host}")
        self.log.info(res3.json())
        self.assertEqual(1, res3.json()['total'], '服务器添加失败')

        text = '----step2:添加用户 expect:成功----'
        self.log.info(text)
        self.data3 = {"hostId": f"{res3.json()['rows'][0]['hostId']}",
                      "username": f"{self.pri_node.ssh_user}",
                      "password": f"{self.pri_node.ssh_password}"}
        res4 = self.com_api.post_hostUser(self.data3)
        self.log.info(res4.json())
        self.assertEqual(200, res4.json()['code'], '添加用户失败')

        text = '----step3:获取并行安装服务器列表 expect:成功----'
        self.log.info(text)
        res5 = self.base_ops_api.get_plugins_base_ops_clusterTask_host_list()
        self.log.info(res5.text)
        self.assertTrue(f"{self.server_name}" in res5.text and
                        f"{self.pri_node.ssh_host}" in res5.text, "获取服务器列表失败")

    def tearDown(self):
        text = '----step3:查询服务器是否存在 expect：成功----'
        self.log.info(text)
        res6 = self.com_api.get_host_page(name=f"{self.pri_node_root.ssh_host}")
        self.log.info(res6.json())
        self.assertFalse(res6.json()['total'] > 1, '服务器管理功能异常')
        if res6.json()['total'] == 1:
            self.log.info('----清理环境,删除服务器----')
            res7 = self.com_api.delete_host(res6.json()['rows'][0]['hostId'])
            self.log.info(res7.json())
            self.assertEqual(200, res7.json()['code'], '删除服务器失败')
            res8 = self.com_api.get_host_page(name=f'{self.pri_node_root.ssh_host}')
            self.log.info(res8.json())
            self.assertEqual(0, res8.json()['total'], '删除服务器异常')
        else:
            self.log.info('----无需清理环境----')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
